//
//  ViewController.m
//  09-UITableView省市区
//
//  Created by LL.F on 16/7/26.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "ViewController.h"
#define kScreenbounds [UIScreen mainScreen].bounds
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight  [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *proTableView;
@property (nonatomic, strong) NSMutableArray *areaArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.title = @"三层省市区";
    self.view.backgroundColor = [UIColor colorWithRed:1.000 green:0.695 blue:0.941 alpha:1.000];
    
    self.navigationController.hidesBarsOnSwipe = YES;
    
    self.areaArray = [NSMutableArray array];
    // 处理数据
    NSString *string = [NSString stringWithContentsOfFile:@"/Users/LL.F/DLS160710/UI/HomeWork/09-UITableView省市区/09-UITableView省市区/area.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *array = [string componentsSeparatedByString:@"\n"];
    //NSLog(@"%@", array);
    for (NSString *str in array) {
        if (![str hasPrefix:@"  "]) {
            NSMutableDictionary *provinceDic = [NSMutableDictionary dictionary];
            [provinceDic setObject:str forKey:@"proName"];
            NSMutableArray *cityArray = [NSMutableArray array];
            [provinceDic setObject:cityArray forKey:@"cityArray"];
            // NSLog(@"%@", provinceDic);
            [self.areaArray addObject:provinceDic];
            
        } else if (![str hasPrefix:@"    "]) {
            
            // 获取到当前市的省
            NSMutableDictionary *provinceDic = [self.areaArray lastObject];
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
            [cityDic setObject:str forKey:@"cityName"];
            NSMutableArray *zoneArray = [NSMutableArray array];
            [cityDic setObject:zoneArray forKey:@"zoneArray"];
            NSMutableArray *cityArray = [provinceDic objectForKey:@"cityArray"];
            [cityArray addObject:cityDic];
        } else {
            
            NSMutableDictionary *provinceDic = [self.areaArray lastObject];
            NSMutableArray *cityArray = [provinceDic objectForKey:@"cityArray"];
            NSMutableDictionary *cityDic = [cityArray lastObject];
            NSMutableArray *zoneArray = [cityDic objectForKey:@"zoneArray"];
            [zoneArray addObject:str];
        }
    }
    
    //NSLog(@"%@", self.areaArray);
    
    //初始化tableView
    self.proTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth , kScreenHeight - 49) style:UITableViewStylePlain];
    [self.view addSubview:_proTableView];
    self.proTableView.delegate = self;
    self.proTableView.dataSource = self;
    [self.proTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    

    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [[self.areaArray objectAtIndex:section] count];
    
}
// cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        UITableViewCell *cell = [self.proTableView dequeueReusableCellWithIdentifier:@"cell"];
        NSMutableDictionary *provinceDic = [self.areaArray objectAtIndex:indexPath.section];
        NSMutableArray *cityArray = [provinceDic objectForKey:@"cityArray"];
        NSMutableDictionary *cityDic = [cityArray objectAtIndex:indexPath.row];
     NSLog(@"%@",cityDic);
        cell.textLabel.text = [cityDic objectForKey:@"cityName"];
        return cell;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
